#!/bin/sh -x

##AUTHOR: Piyush Garg
##Student ID: S3218012
##Yallara Login Name: pgrag

######Declaration of variables for the commands to be used.

GPG=gpg
RM=/usr/bin/rm
EGREP=/usr/bin/egrep
AWK=/usr/bin/awk
SED=/usr/bin/sed
MV=/usr/bin/mv
ORGFILE=datafile
TEMPFILE=encfile.gpg
TEMP=tmp

########Creating the tmp\ directory in users home directory and creating the file inside tmp\

#echo $user | mkdir $TEMP
#cd $TEMP
#touch $ORGFILE

##Creating the function for Encrypting the file to $TEMPFILE

$GPG --output $TEMPFILE --encrypt --default-recipient-self $ORGFILE
$RM $ORGFILE

##Creation of Add_Entry() inorder to provide the functionalities.
Add_Entry()
{
##Asking the user to enter the following details.

echo "Enter Machine name or URL."
read machinename
printf "\n"

echo "Enter Userid."
read userid
printf "\n"

echo "Enter Password."
read password
printf "\n"

echo "Enter Comments."
read comment
printf "\n"

if [ -f $TEMPFILE ] ;then
	
	##Asking the user to enter the passphrase to Decrypt the file.

	echo "Enter the Passphrase for the file."
	read PASSPHRASE
	echo $PASSPHRASE | $GPG --passphrase-fd 0 --output $ORGFILE --decrypt -n $TEMPFILE
	$RM $TEMPFILE

fi

##Putting all the data in the $ORGFILE.

echo "$machinename/$userid/$password/$comment" >> $ORGFILE

ENCRYPT_FILE			##Encrypting the file.

echo "Press Enter to go back to the Menu."
read exit

}

##Function for looking up UserId and Password Combination.
Look_up_combination()
{
if [ -f $TEMPFILE ] ;then

        ##Asking the user to enter the passphrase to Decrypt the file.#

        echo "Enter the Passphrase for the file."
        read PASSPHRASE
        echo $PASSPHRASE | $GPG --passphrase-fd 0 --output $ORGFILE --decrypt -n $TEMPFILE
        $RM $TEMPFILE

	echo "Enter the System Name to get the UserId and Password Combination."	##Works only with words with no spaces.
	read SEARCHSYSTEMNAME
	
	SEARCHSTRING=`$EGREP $SEARCHSYSTEMNAME $ORGFILE | $AWK -F/ '{print $1}'`
	
	if [ "$SEARCHSTRING" ] ;then
	
        	SYSNAME=`echo $SEARCHSTRING | $AWK -F/ '{print $1}'`

	        if [ $SEARCHSTRING = $SEARCHSYSTEMNAME ] ;then  ##Comapring if the User Input matches with System Name.
	
        	        USER_ID=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $2}'`  ##UserId.	
        	        PASSWORD=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $3}'` ##Password.

                	printf "\n"
	                echo "UserId: $USER_ID"
        	        echo "Password: $PASSWORD"
                	printf "\n"
			
			ENCRYPT_FILE					 ##Encrypting the file.

	                echo "Press Enter to go back to the Menu."
        	        read exit

	        else
        	         echo "$SEARCHSYSTEMNAME is not a System Name."
                         ENCRYPT_FILE					##Encrypting the file.

                	 echo "Press Enter to go back to the Menu."
	                 read exit
        	fi

	else
        	echo "$SEARCHSYSTEMNAME not found."
		ENCRYPT_FILE						##Encrypting the file.

	        echo "Press Enter to go back to the Menu."
        	read exit
	fi

else
	echo "Database does not exists."	
	
	##Using created function to create DATABASE if not exists.
	CREATEDB
fi

}

##Modify Function used for modifying the data.
Modify_data()
{
if [ -f $TEMPFILE ] ;then

        ##Asking the user to enter the passphrase to Decrypt the file.#

	echo "Enter the Passphrase for the file."
	read PASSPHRASE
	echo $PASSPHRASE | $GPG --passphrase-fd 0 --output $ORGFILE --decrypt -n $TEMPFILE
	$RM $TEMPFILE

# 	cat $PASSPHRASE_FILE | $GPG --passphrase-fd 0 --output $ORGFILE --decrypt -n $TEMPFILE
#       $RM $TEMPFILE

	echo "Enter the System Name to change the password."		##Works only with words with no spaces.
	read SEARCHSYSTEMNAME

	SEARCHSTRING=`$EGREP $SEARCHSYSTEMNAME $ORGFILE | $AWK -F/ '{print $1}'`  ##Grabbing the System Name from the database file.

	if [ "$SEARCHSTRING" ] ;then

        	SYSNAME=`echo $SEARCHSTRING | $AWK -F/ '{print $1}'`

	        if [ $SEARCHSTRING = $SEARCHSYSTEMNAME ] ;then			  ## If User Input matches with System Name then
	
        	        SYSTEM_NAME=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $1}'`  ##System Name
			USER_ID=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $2}'`	     ##User ID
        	        PASSWORD=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $3}'`     ##PASSWORD
			COMMENTS=`$EGREP $SEARCHSTRING $ORGFILE | $AWK -F/ '{print $4}'`     ##Comments

 			echo "Name: $SYSTEM_NAME"		##Displaying the Details according to System Name
			echo "UserId: $USER_ID"
        	        echo "Password: $PASSWORD"
			echo "Comments: $COMMENTS"
			printf "\n"
			
			echo "Change Password: "
			read CHANGE_PASSWORD

			$SED "/$SEARCHSTRING/d" $ORGFILE > ORIGDATA
			$RM $ORGFILE
			$MV -f ORIGDATA $ORGFILE
			$RM ORIGDATA
									
			##Putting all the data in the $ORGFILE.
			echo "$SYSTEM_NAME/$USER_ID/$CHANGE_PASSWORD/$COMMENTS" >> $ORGFILE

			ENCRYPT_FILE						 ## Encrypting the database file

			echo "Press Enter to go back to the Menu."
        	        read exit

	        else
        	        echo "$SEARCHSYSTEMNAME is not a System Name."
			ENCRYPT_FILE						 ##Encrypting the file.

	                echo "Press Enter to go back to the Menu."
        	        read exit
	        fi

	else
        	echo "$SEARCHSYSTEMNAME not found."
  		ENCRYPT_FILE							 ##Encrypting the file.

	        echo "Press Enter to go back to the Menu."
        	read exit
	fi

else
        echo "Database does not exists."

        ##Using created function to create DATABASE if not exists.
	CREATEDB

fi

}

##Creating Database if not exists
CREATEDB()
{
printf "\n"
echo "Do you want to create a database(y/n)."       ##Asking the User if he want to create database if not exist
read CREATE_DATABASE

case $CREATE_DATABASE in
	'y')Add_Entry;;
        'n')mainmenu;;
        *)  echo "Press Enter to go back to the Menu."
            read exit;;
esac

}

##Function for Encrypting the file
ENCRYPT_FILE()
{
##Encrypting the file for the security reasons.
$GPG --output $TEMPFILE --encrypt --default-recipient-self $ORGFILE
$RM $ORGFILE	
}


#Building the Main Menu
mainmenu()
{
#Clearing the Screen

clear
	
#Menu Options
printf "******************************************\n"
printf "\n"
printf "Encryption File Management System.\n"
printf "--------------------------------------\n"
printf "\n"
printf "1)Add an entry for System or Account.\n"
printf "2)Lookup Userid and Password Combination.\n"
printf "3)Modify the stored data.\n"
printf "4)Exit.\n"
printf "Enter your Choice:\n"
printf "\n"
printf "******************************************\n"

read response

if [ $? = 0 ];then

	#eval is for reference for response
	eval $1=\${response}
	return 0
fi

}

#Functions for the Menu options selected
while true

do
        mainmenu option

        case "$option" in

		#Functions for every menu Item, the user selects.
                "1")Add_Entry;;
                "2")Look_up_combination;;
                "3")Modify_data;;
                "4")exit 0;;
                  *)echo "Invaid Option. Please press Enter to Continue...\n"
                        read input;;
        esac

done

##NOTES##
#**********************************************************
#My Database data will reside in the following data:
#systemname/userid/password/comments
#
#Dell/piyush/qawse/good
#Lenovo/John/qwe/Good
#Acer/abhi/abhig/Hello
#Sun/pgrag/s3218012/LabComp
#**********************************************************
#
#*************************************************************NOTES********************************************************************************
#If you want to test with your own encrypted file then you have to provide your own file in $TEMP and the file name to which it decrypts in $ORGFILE
#**************************************************************************************************************************************************
#
#In this assignment i have asked the user to enter the passphrase to decrypt the file with controlling the popup window.
#In the Specification we were said to store the file in some of the specific location under /home directory.
#I thought this could be the way to store the temporary files and database files but at last database file remains.


#########Reference: ###########

##Text Books##
#1. Essential System Administration by 'O'Reilly'
#2. Unix User Manual
#
##WebSites##
#1. http://www.unix.com/shell-programming-scripting/63964-delete-lines-unix.html
#2. http://www.unix.com/shell-programming-scripting/31181-delete-line-file-sed.html
#3. www.gnupg.org/
#4. www.cyberciti.biz/.../linux-how-to-encrypt-and-decrypt-files-with-a-password.html 
